package parsers;

import java.util.List;

import object.Course;
import object.PersonInfo;
import object.ScoreList;
import object.Student;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class StudentParser {

	private Student student;
	
	public StudentParser(){
		
	}
	
	public StudentParser(Student student){
		this.student=student;
	}
	
	public void setStudent(Student student){
		this.student=student;
	}
	
	public Element parse(Document doc){
		if(doc==null||student==null){
			System.err.println("doc or student cannot be null!");
			return null;
		}
		
		String sid=student.getSid();
		PersonInfo pinfo=student.getPinfo();
		String grade=student.getGrade();
		List<Course> courses=student.getCourseList();
		ScoreList scores=student.getScoreList();
		
		Element studentE=doc.createElement("学生信息");
		
		Element sidE=doc.createElement("学号");
		studentE.appendChild(sidE);
		Text sidT=doc.createTextNode(sid);
		sidE.appendChild(sidT);
		
		PersonInfoParser pParser=new PersonInfoParser(pinfo);
		Element pinfoE=pParser.parse(doc);
		studentE.appendChild(pinfoE);
		
		if(grade!=null){
			Element gradeE=doc.createElement("学级");
			studentE.appendChild(gradeE);
			Text gradeT=doc.createTextNode(grade);
			gradeE.appendChild(gradeT);
		}
		
		if(courses!=null&&courses.size()!=0){
			Element courseListE=doc.createElement("课程信息");
			studentE.appendChild(courseListE);
			CourseParser cParser=new CourseParser();
			for(Course c:courses){
				cParser.setCourse(c);
				Element courseE=cParser.parse(doc);
				courseListE.appendChild(courseE);
			}
		}
		
		if(scores!=null){
			ScoreListParser sParser=new ScoreListParser(scores);
			Element scoresE=sParser.parse(doc);
			studentE.appendChild(scoresE);
		}
		
		return studentE;
	}
	
}
